package main;

import javax.sound.midi.Sequence;
import javax.sound.sampled.AudioInputStream;

import control.MidiPlayer;
import control.WavPlayer;

public class Audio {
	private static MidiPlayer mp = new MidiPlayer();
	private static boolean enabled;
	private static Thread thread;
	
	public static void playMIDI (final Sequence midi, final boolean looping)
	{
		if (enabled) {
			thread = new Thread() 
			{
				@Override
				public void run ()
				{
					mp.stop();
					mp.play(midi, looping);
				}
			};
			thread.start();
		}
	}
	public static void stopMIDI ()
	{
		if (enabled)
		{
			thread.interrupt();
			mp.stop();
		}
	}
	public static void terminate ()
	{
		stopMIDI();
		mp.close();
	}
	public static void playWAV (AudioInputStream wav)
	{
		if (enabled) {
			WavPlayer.play(wav);
		}
	}
	
	
	public static boolean isPlayingMidi ()
	{
		return mp.getSequencer().isRunning();
	}
	/**
	 * @return the enabled
	 */
	public static boolean isEnabled() {
		return enabled;
	}
	/**
	 * @param enabled the enabled to set
	 */
	public static void setEnabled(boolean enabled) {
		Audio.enabled = enabled;
	}
}
